<?php

use Nette\Application\UI;


/**
 * Sign in/out presenters.
 *
 * @author     John Doe
 * @package    MyApplication
 */
final class SearchPresenter extends BasePresenter
{
    /** @persistent */
    public $gender;
    public $minAge;
    public $maxAge;
    
    public function renderDefault()
    {
	$vp = $this['vp'];
	
	$row = $this->model->getUsers()->where(array('gender' => $this->gender));
	
	$paginator = $vp->paginator;
	$paginator->itemsPerPage = 4;
	$paginator->itemCount = $row->count();

	$this->template->results = $row->limit($paginator->getLength(), $paginator->getOffset());
    }

    protected function createComponentVp()
    {
	return new VisualPaginator;
    }
    
	/**
	 * Sign in form component factory.
	 * @return Nette\Application\UI\Form
	 */
	protected function createComponentSearchForm()
	{
		$form = new UI\Form;
		
		$gender = array(
		    '1' => 'Muž',
		    '0' => 'Žena'
		);
		$form->addSelect('gender', 'Pohlavie:', $gender)
		    ->setRequired('Prosim zvolte pohlavie !')
		    ->setDefaultValue($this->gender);

		$form->addSubmit('send', 'Hladat');
		
		$form->addProtection('Vyprsala platnost, zadajte este raz');

		$form->onSuccess[] = callback($this, 'searchFormSubmitted');
		return $form;
	}



	public function SearchFormSubmitted($form)
	{
		try {
			$values = $form->getValues();	
			
			$this->redirect('Search:', array('gender' => $form->values->gender));

		} catch (RuntimeException $e) {
			$form->addError($e->getMessage());
		}
	}
}
